﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
public partial class Main_Inventory_IssuedItemReturn : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    int tblCounter = 0;
    List<string> LstID = new List<string>();
    List<string> LstItemName = new List<string>();
    List<string> LstItemDesc = new List<string>();
    List<string> LstRegID = new List<string>();
    List<string> LstRO = new List<string>();
    List<string> LstCustomer = new List<string>();
    List<string> LstCustID = new List<string>();
    List<string> LstProjectName = new List<string>();
    List<string> LstProjectId = new List<string>();
    List<string> LstIssuedQty = new List<string>();
    List<string> LstReturnQty = new List<string>();
    List<string> LstTITID = new List<string>();
    List<string> LstItemId = new List<string>();
    List<string> LstItemQty = new List<string>();
    List<string> LstPrevReturn = new List<string>();
    List<string> LstBalance = new List<string>();
    DataTable DTvaluation = new DataTable();
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        LoadViewStateControl();
    }
    protected void LoadViewStateControl()
    {
        LstID = (List<string>)ViewState["LstID"];
        LstItemName = (List<string>)ViewState["LstItemName"];
        LstItemDesc = (List<string>)ViewState["LstItemDesc"];
        LstRegID = (List<string>)ViewState["LstRegID"];
        LstRO = (List<string>)ViewState["LstRO"];
        LstCustomer = (List<string>)ViewState["LstCustomer"];
        LstCustID = (List<string>)ViewState["LstCustID"];
        LstProjectName = (List<string>)ViewState["LstProjectName"];
        LstProjectId = (List<string>)ViewState["LstProjectId"];
        LstIssuedQty = (List<string>)ViewState["LstIssuedQty"];
        LstReturnQty = (List<string>)ViewState["LstReturnQty"];
        LstTITID = (List<string>)ViewState["LstTITID"];
        LstItemId = (List<string>)ViewState["LstItemId"];
        LstItemQty = (List<string>)ViewState["LstItemQty"];
        LstPrevReturn = (List<string>)ViewState["LstPrevReturn"];
        LstBalance = (List<string>)ViewState["LstBalance"];
        if (Session["hidCounter"] != null)
        {
            int count = Convert.ToInt32(Session["hidCounter"].ToString());
            for (int i = 0; i < count; i++)
            {
                HtmlTableRow htRow = new HtmlTableRow();

                HtmlTableCell htcell1 = new HtmlTableCell();
                TextBox txtItemname = new TextBox();
                txtItemname.ID = LstItemName[i].ToString();
                txtItemname.Width = Unit.Percentage(99);
                txtItemname.CssClass = "labelText";
                txtItemname.ForeColor = System.Drawing.Color.Black;
                txtItemname.Enabled = false;
                HiddenField Hid = new HiddenField();
                Hid.ID = LstID[i].ToString();
                htcell1.Controls.Add(Hid);
                htcell1.Controls.Add(txtItemname);

                HtmlTableCell htcell2 = new HtmlTableCell();
                TextBox txtItemdesc = new TextBox();
                txtItemdesc.ID = LstItemDesc[i].ToString();
                txtItemdesc.Width = Unit.Percentage(99);
                txtItemdesc.CssClass = "labelText";
                txtItemdesc.ForeColor = System.Drawing.Color.Black;
                txtItemdesc.Enabled = false;
                htcell2.Controls.Add(txtItemdesc);

                HtmlTableCell htcell3 = new HtmlTableCell();
                HiddenField hidregid = new HiddenField();
                hidregid.ID = LstRegID[i].ToString();
                htcell3.Controls.Add(hidregid);

                HtmlTableCell htcell6 = new HtmlTableCell();
                TextBox txtro = new TextBox();
                txtro.ID = LstRO[i].ToString();
                txtro.Width = Unit.Percentage(99);
                txtro.CssClass = "labelText";
                txtro.ForeColor = System.Drawing.Color.Black;
                txtro.Enabled = false;
                htcell6.Controls.Add(txtro);

                HtmlTableCell htcell7 = new HtmlTableCell();
                TextBox txtcustomer = new TextBox();
                txtcustomer.ID = LstCustomer[i].ToString();
                txtcustomer.Width = Unit.Percentage(99);
                txtcustomer.CssClass = "labelText";
                txtcustomer.ForeColor = System.Drawing.Color.Black;
                txtcustomer.Enabled = false;
                HiddenField hidcustid = new HiddenField();
                hidcustid.ID = LstCustID[i].ToString();
                htcell7.Controls.Add(hidcustid);
                htcell7.Controls.Add(txtcustomer);

                HtmlTableCell htcell8 = new HtmlTableCell();
                TextBox txtprojectname = new TextBox();
                txtprojectname.ID = LstProjectName[i].ToString();
                txtprojectname.Width = Unit.Percentage(99);
                txtprojectname.CssClass = "labelText";
                txtprojectname.ForeColor = System.Drawing.Color.Black;
                txtprojectname.Enabled = false;
                HiddenField Hidprojectid = new HiddenField();
                Hidprojectid.ID = LstProjectId[i].ToString();
                htcell8.Controls.Add(Hidprojectid);
                htcell8.Controls.Add(txtprojectname);

                HtmlTableCell htcell9 = new HtmlTableCell();
                TextBox txtIssueQty = new TextBox();
                txtIssueQty.ID = LstIssuedQty[i].ToString();
                txtIssueQty.Width = Unit.Percentage(99);
                txtIssueQty.CssClass = "labelText";
                txtIssueQty.Style["text-align"] = "right";
                txtIssueQty.ForeColor = System.Drawing.Color.Black;
                txtIssueQty.Enabled = false;
                htcell9.Controls.Add(txtIssueQty);

                HtmlTableCell htcell10 = new HtmlTableCell();
                TextBox txtReturnQty = new TextBox();
                txtReturnQty.ID = LstReturnQty[i].ToString();
                txtReturnQty.Width = Unit.Percentage(99);
                txtReturnQty.CssClass = "labelText";
                txtReturnQty.Style["text-align"] = "right";
                txtReturnQty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
                txtReturnQty.ForeColor = System.Drawing.Color.Black;
                htcell10.Controls.Add(txtReturnQty);

                HtmlTableCell htcell11 = new HtmlTableCell();
                HiddenField HidTitid = new HiddenField();
                HidTitid.ID = LstTITID[i].ToString();
                htcell11.Controls.Add(HidTitid);

                HtmlTableCell htcell12 = new HtmlTableCell();
                TextBox txtPrevReturn = new TextBox();
                txtPrevReturn.ID = LstPrevReturn[i].ToString();
                txtPrevReturn.Width = Unit.Percentage(99);
                txtPrevReturn.CssClass = "labelText";
                txtPrevReturn.Enabled = false;
                txtPrevReturn.Style["text-align"] = "right";
                txtPrevReturn.ForeColor = System.Drawing.Color.Black;
                htcell12.Controls.Add(txtPrevReturn);

                HtmlTableCell htcell13 = new HtmlTableCell();
                TextBox txtBalance = new TextBox();
                txtBalance.ID = LstBalance[i].ToString();
                txtBalance.Width = Unit.Percentage(99);
                txtBalance.CssClass = "labelText";
                txtBalance.Enabled = false;
                txtBalance.Style["text-align"] = "right";
                txtBalance.ForeColor = System.Drawing.Color.Black;
                htcell13.Controls.Add(txtBalance);

                HtmlTableCell htcell14 = new HtmlTableCell();
                HiddenField HidItemId = new HiddenField();
                HidItemId.ID = LstItemId[i].ToString();
                htcell14.Controls.Add(HidItemId);

                HtmlTableCell htcell15 = new HtmlTableCell();
                HiddenField HidItemQty = new HiddenField();
                HidItemQty.ID = LstItemQty[i].ToString();
                htcell11.Controls.Add(HidItemQty);

                htRow.Cells.Add(htcell1);
                htRow.Cells.Add(htcell2);
                htRow.Cells.Add(htcell3);
                htRow.Cells.Add(htcell6);
                htRow.Cells.Add(htcell7);
                htRow.Cells.Add(htcell8);
                htRow.Cells.Add(htcell9);
                htRow.Cells.Add(htcell12);
                htRow.Cells.Add(htcell13);
                htRow.Cells.Add(htcell10);
                htRow.Cells.Add(htcell11);
                htRow.Cells.Add(htcell14);
                htRow.Cells.Add(htcell15);
                tbldetail.Rows.Add(htRow);
            }
        }
    }
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetSlipNo(string prefixText)
    {
        Datautility db = new Datautility();
        DataTable dt = db.getdata("select distinct ORS.slipno from IVR_OnlineRegistrationSlip ORS inner join IVR_OnlineRegistration_Detail ORD on ORS.RegId=ORD.RegId  where ISNULL(ORD.IssueQty,0) > 0 and ORS.slipno like '%" + prefixText + "%'");
        List<string> SlipNo = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            SlipNo.Add(Convert.ToString(dt.Rows[i]["slipno"]));
        }
        return SlipNo;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            string Pagepermission = "select PAGE_ID,PAGE_ADD,PAGE_VIEW,USERID,USERNAME as EmailID from USER_PERMISSION inner join LOGIN on USER_PERMISSION.USER_ID=LOGIN.USERID where USER_ID='" + Session["userid"].ToString() + "' and PAGE_ID=1034";
            DataTable dt = DU.getdata(Pagepermission);
            if (dt.Rows.Count > 0)
            {
                if (Request.QueryString["Mode"] == "View")
                {
                    if (dt.Rows[0]["PAGE_VIEW"].ToString() == "True")
                    {
                        pnldetailview.Visible = true;
                        pnl_Detail.Visible = false;
                        pnlslipNo.Visible = false;
                        ViewRetrunData();
                    }
                    else
                    {
                        Response.Redirect("~/AccessDenied.aspx");
                    }
                }
                if (Request.QueryString["Mode"] == "addnew")
                {
                    if (dt.Rows[0]["PAGE_ADD"].ToString() == "True")
                    {
                        pnlslipNo.Visible = true;
                        pnl_Detail.Visible = false;
                        pnldetailview.Visible = false;
                    }
                    else
                    {
                        Response.Redirect("~/AccessDenied.aspx");
                    }
                }
            }
        }
    }
    private void Addrow()
    {
        tblCounter++;

        HtmlTableRow htRow = new HtmlTableRow();

        HtmlTableCell htcell1 = new HtmlTableCell();
        TextBox txtItemname = new TextBox();
        txtItemname.ID = "txtItemname" + tblCounter.ToString();
        txtItemname.Width = Unit.Percentage(99);
        txtItemname.CssClass = "labelText";
        txtItemname.ForeColor = System.Drawing.Color.Black;
        txtItemname.Enabled = false;
        HiddenField Hid = new HiddenField();
        Hid.ID = "Hid" + tblCounter.ToString();
        htcell1.Controls.Add(Hid);
        htcell1.Controls.Add(txtItemname);
        LstID.Add(Hid.ID);
        LstItemName.Add(txtItemname.ID);

        HtmlTableCell htcell2 = new HtmlTableCell();
        TextBox txtItemdesc = new TextBox();
        txtItemdesc.ID = "txtItemdesc" + tblCounter.ToString();
        txtItemdesc.Width = Unit.Percentage(99);
        txtItemdesc.CssClass = "labelText";
        txtItemdesc.ForeColor = System.Drawing.Color.Black;
        txtItemdesc.Enabled = false;
        htcell2.Controls.Add(txtItemdesc);
        LstItemDesc.Add(txtItemdesc.ID);

        HtmlTableCell htcell3 = new HtmlTableCell();
        HiddenField hidregid = new HiddenField();
        hidregid.ID = "hidregid" + tblCounter.ToString();
        htcell3.Controls.Add(hidregid);
        LstRegID.Add(hidregid.ID);

        HtmlTableCell htcell6 = new HtmlTableCell();
        TextBox txtro = new TextBox();
        txtro.ID = "txtro" + tblCounter.ToString();
        txtro.Width = Unit.Percentage(99);
        txtro.CssClass = "labelText";
        txtro.ForeColor = System.Drawing.Color.Black;
        txtro.Enabled = false;
        htcell6.Controls.Add(txtro);
        LstRO.Add(txtro.ID);

        HtmlTableCell htcell7 = new HtmlTableCell();
        TextBox txtcustomer = new TextBox();
        txtcustomer.ID = "txtcustomer" + tblCounter.ToString();
        txtcustomer.Width = Unit.Percentage(99);
        txtcustomer.CssClass = "labelText";
        txtcustomer.ForeColor = System.Drawing.Color.Black;
        txtcustomer.Enabled = false;
        HiddenField hidcustid = new HiddenField();
        hidcustid.ID = "hidcustid" + tblCounter.ToString();
        htcell7.Controls.Add(hidcustid);
        htcell7.Controls.Add(txtcustomer);
        LstCustID.Add(hidcustid.ID);
        LstCustomer.Add(txtcustomer.ID);

        HtmlTableCell htcell8 = new HtmlTableCell();
        TextBox txtprojectname = new TextBox();
        txtprojectname.ID = "txtprojectname" + tblCounter.ToString();
        txtprojectname.Width = Unit.Percentage(99);
        txtprojectname.CssClass = "labelText";
        txtprojectname.ForeColor = System.Drawing.Color.Black;
        txtprojectname.Enabled = false;
        HiddenField Hidprojectid = new HiddenField();
        Hidprojectid.ID = "Hidprojectid" + tblCounter.ToString();
        htcell8.Controls.Add(Hidprojectid);
        htcell8.Controls.Add(txtprojectname);
        LstProjectId.Add(Hidprojectid.ID);
        LstProjectName.Add(txtprojectname.ID);

        HtmlTableCell htcell9 = new HtmlTableCell();
        TextBox txtIssueQty = new TextBox();
        txtIssueQty.ID = "txtIssueQty" + tblCounter.ToString();
        txtIssueQty.Width = Unit.Percentage(99);
        txtIssueQty.CssClass = "labelText";
        txtIssueQty.Style["text-align"] = "right";
        txtIssueQty.ForeColor = System.Drawing.Color.Black;
        txtIssueQty.Enabled = false;
        htcell9.Controls.Add(txtIssueQty);
        LstIssuedQty.Add(txtIssueQty.ID);

        HtmlTableCell htcell10 = new HtmlTableCell();
        TextBox txtReturnQty = new TextBox();
        txtReturnQty.ID = "txtReturnQty" + tblCounter.ToString();
        txtReturnQty.Width = Unit.Percentage(99);
        txtReturnQty.CssClass = "labelText";
        txtReturnQty.Text = "0";
        txtReturnQty.Style["text-align"] = "right";
        txtReturnQty.Attributes.Add("onfocus", "if(this.value=='0') this.value='';");
        txtReturnQty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
        txtReturnQty.Attributes.Add("onblur", "return CheckValue(" + tblCounter.ToString() + ");");
        txtReturnQty.ForeColor = System.Drawing.Color.Black;
        htcell10.Controls.Add(txtReturnQty);
        LstReturnQty.Add(txtReturnQty.ID);

        HtmlTableCell htcell11 = new HtmlTableCell();
        HiddenField HidTitid = new HiddenField();
        HidTitid.ID = "HidTitid" + tblCounter.ToString();
        htcell11.Controls.Add(HidTitid);
        LstTITID.Add(HidTitid.ID);

        HtmlTableCell htcell12 = new HtmlTableCell();
        TextBox txtPrevReturn = new TextBox();
        txtPrevReturn.ID = "txtPrevReturn" + tblCounter.ToString();
        txtPrevReturn.Width = Unit.Percentage(99);
        txtPrevReturn.CssClass = "labelText";
        txtPrevReturn.Enabled = false;
        txtPrevReturn.Style["text-align"] = "right";
        txtPrevReturn.ForeColor = System.Drawing.Color.Black;
        htcell12.Controls.Add(txtPrevReturn);
        LstPrevReturn.Add(txtPrevReturn.ID);

        HtmlTableCell htcell13 = new HtmlTableCell();
        TextBox txtBalance = new TextBox();
        txtBalance.ID = "txtBalance" + tblCounter.ToString();
        txtBalance.Width = Unit.Percentage(99);
        txtBalance.CssClass = "labelText";
        txtBalance.Style["text-align"] = "right";
        txtBalance.Enabled = false;
        txtBalance.ForeColor = System.Drawing.Color.Black;
        htcell13.Controls.Add(txtBalance);
        LstBalance.Add(txtBalance.ID);

        HtmlTableCell htcell14 = new HtmlTableCell();
        HiddenField HidItemId = new HiddenField();
        HidItemId.ID = "HidItemId" + tblCounter.ToString();
        htcell14.Controls.Add(HidItemId);
        LstItemId.Add(HidItemId.ID);

        HtmlTableCell htcell15 = new HtmlTableCell();
        HiddenField HidItemQty = new HiddenField();
        HidItemQty.ID = "HidItemQty" + tblCounter.ToString();
        htcell11.Controls.Add(HidItemQty);
        LstItemQty.Add(HidItemQty.ID);

        htRow.Cells.Add(htcell1);
        htRow.Cells.Add(htcell2);
        htRow.Cells.Add(htcell3);
        htRow.Cells.Add(htcell6);
        htRow.Cells.Add(htcell7);
        htRow.Cells.Add(htcell8);
        htRow.Cells.Add(htcell9);
        htRow.Cells.Add(htcell12);
        htRow.Cells.Add(htcell13);
        htRow.Cells.Add(htcell10);
        htRow.Cells.Add(htcell11);
        htRow.Cells.Add(htcell14);
        htRow.Cells.Add(htcell15);
        tbldetail.Rows.Add(htRow);

        ViewState["LstID"] = LstID;
        ViewState["LstItemName"] = LstItemName;
        ViewState["LstItemDesc"] = LstItemDesc;
        ViewState["LstRegID"] = LstRegID;
        ViewState["LstRO"] = LstRO;
        ViewState["LstCustomer"] = LstCustomer;
        ViewState["LstCustID"] = LstCustID;
        ViewState["LstProjectName"] = LstProjectName;
        ViewState["LstProjectId"] = LstProjectId;
        ViewState["LstIssuedQty"] = LstIssuedQty;
        ViewState["LstReturnQty"] = LstReturnQty;
        ViewState["LstTITID"] = LstTITID;
        ViewState["LstItemId"] = LstItemId;
        ViewState["LstItemQty"] = LstItemQty;
        ViewState["LstPrevReturn"] = LstPrevReturn;
        ViewState["LstBalance"] = LstBalance;

        Session["hidCounter"] = tblCounter.ToString();
        hidIssuedItemDetail.Value = tblCounter.ToString();
    }
    private void _DeleteRows()
    {
        LstItemName = (List<string>)ViewState["LstItemName"];
        LstItemDesc = (List<string>)ViewState["LstItemDesc"];
        LstIssuedQty = (List<string>)ViewState["LstIssuedQty"];
        LstReturnQty = (List<string>)ViewState["LstReturnQty"];
        LstTITID = (List<string>)ViewState["LstTITID"];
        for (int i = tbldetail.Rows.Count; i > 1; i--)
        {
            if (Session["hidCounter"] != null)
            {
                LstItemName.RemoveAt(i - 2);
                LstItemDesc.RemoveAt(i - 2);
                LstIssuedQty.RemoveAt(i - 2);
                LstReturnQty.RemoveAt(i - 2);
                LstTITID.RemoveAt(i - 2);
            }
            tbldetail.Rows.RemoveAt(i - 1);
        }
        tblCounter = 0;
    }
    private void GetData()
    {
        string sqlSlipDetails = "select * from IVR_ISSUEITEM  where SLIPNO=" + txtslipno.Text;
        DataTable DtSlipDetails = DU.getdata(sqlSlipDetails);
        if (DtSlipDetails.Rows.Count > 0)
        {
            txtrequestedBy.Text = DtSlipDetails.Rows[0]["RequestedBy"].ToString().ToUpper();
            TxtissuedBy.Text = DtSlipDetails.Rows[0]["IssueBy"].ToString();
            txtissuedate.Text = Convert.ToDateTime(DtSlipDetails.Rows[0]["IssueDate"].ToString()).ToString("dd-MM-yyyy");
            BindUsername();
            string sqlItemDetails = "select ISSUEID,REGID,ITMID,ITMCODE,ITEMDESC,RO,MAS_PROJECT.projectname as ProjectName,CUSTOMER,PROJECT,ACC_LEDMAST.LANAME as CustomerName from IVR_ISSUEITEM_DETAILS inner join MAS_PROJECT on MAS_PROJECT.projectid=IVR_ISSUEITEM_DETAILS.PROJECT inner join ACC_LEDMAST On ACC_LEDMAST.LID=IVR_ISSUEITEM_DETAILS.CUSTOMER where IVR_ISSUEITEM_DETAILS.ISSUEID=" + DtSlipDetails.Rows[0]["ISSUEID"].ToString();
            DataTable DtItemDetail = DU.getdata(sqlItemDetails);
            if (DtItemDetail.Rows.Count > 0)
            {
                //if (DtItemDetail.Rows.Count > 1)
                //{
                //    _DeleteRows();
                //}
                for (int i = 0; i < DtItemDetail.Rows.Count; i++)
                {
                    Addrow();
                }
                for (int j = 1; j <= DtItemDetail.Rows.Count; j++)
                {
                    TextBox txtItemCode = (TextBox)tbldetail.Rows[j].FindControl("txtItemname" + j.ToString());
                    TextBox txtItemDesc = (TextBox)tbldetail.Rows[j].FindControl("txtItemdesc" + j.ToString());
                    HiddenField hidregid = (HiddenField)tbldetail.Rows[j].FindControl("hidregid" + j.ToString());
                    TextBox txtpecode = (TextBox)tbldetail.Rows[j].FindControl("txtPeCode" + j.ToString());
                    TextBox txtProductDesc = (TextBox)tbldetail.Rows[j].FindControl("txtproductdesc" + j.ToString());
                    TextBox txtRo = (TextBox)tbldetail.Rows[j].FindControl("txtro" + j.ToString());
                    TextBox txtcustomer = (TextBox)tbldetail.Rows[j].FindControl("txtcustomer" + j.ToString());
                    TextBox txtprojectname = (TextBox)tbldetail.Rows[j].FindControl("txtprojectname" + j.ToString());
                    HiddenField hidCustomerID = (HiddenField)tbldetail.Rows[j].FindControl("hidcustid" + j.ToString());
                    HiddenField hidProjectID = (HiddenField)tbldetail.Rows[j].FindControl("Hidprojectid" + j.ToString());
                    TextBox txtIssuedQty = (TextBox)tbldetail.Rows[j].FindControl("txtIssueQty" + j.ToString());
                    HiddenField hidItemId = (HiddenField)tbldetail.Rows[j].FindControl("HidItemId" + j.ToString());
                    HiddenField hidTitid = (HiddenField)tbldetail.Rows[j].FindControl("HidTitid" + j.ToString());
                    HiddenField hidItemQty = (HiddenField)tbldetail.Rows[j].FindControl("HidItemQty" + j.ToString());
                    TextBox txtBalance = (TextBox)tbldetail.Rows[j].FindControl("txtBalance" + j.ToString());
                    TextBox txtPrevReturn = (TextBox)tbldetail.Rows[j].FindControl("txtPrevReturn" + j.ToString());
                    TextBox TxtReturnQty = (TextBox)tbldetail.Rows[j].FindControl("txtReturnQty" + j.ToString());

                    hidItemId.Value = DtItemDetail.Rows[j - 1]["ITMID"].ToString();
                    txtItemCode.Text = DtItemDetail.Rows[j - 1]["ITMCODE"].ToString();
                    txtItemDesc.Text = DtItemDetail.Rows[j - 1]["ITEMDESC"].ToString();
                    txtRo.Text = DtItemDetail.Rows[j - 1]["RO"].ToString();
                    txtcustomer.Text = DtItemDetail.Rows[j - 1]["CustomerName"].ToString();
                    txtprojectname.Text = DtItemDetail.Rows[j - 1]["ProjectName"].ToString();
                    //txtIssuedQty.Text = DtItemDetail.Rows[j - 1]["ISSUEQTY"].ToString();
                    hidregid.Value = DtItemDetail.Rows[j - 1]["REGID"].ToString();
                    hidCustomerID.Value = DtItemDetail.Rows[j - 1]["CUSTOMER"].ToString();
                    hidProjectID.Value = DtItemDetail.Rows[j - 1]["PROJECT"].ToString();
                    double qty = 0;
                    string sqlIssuedqty = "select ISNULL(SUM(IVR_ISSUEITEM_BATCHDETAILS.OPQTY),0) as OPQTY, ISNULL(sum(IVR_ISSUEITEM_BATCHDETAILS.PURQTY),0) as PURQTY from IVR_ISSUEITEM_BATCHDETAILS inner join IVR_COMMONITEM_ISSUERETURN on IVR_ISSUEITEM_BATCHDETAILS.ISSUEBATCHID=IVR_COMMONITEM_ISSUERETURN.ISSUEBATCHID where IVR_ISSUEITEM_BATCHDETAILS.ITMID=" + hidItemId.Value + " and IVR_COMMONITEM_ISSUERETURN.SLIPNO=" + txtslipno.Text;
                    DataTable DTISSUED = DU.getdata(sqlIssuedqty);
                    if (DTISSUED.Rows.Count > 0)
                    {
                        for (int k = 0; k < DTISSUED.Rows.Count; k++)
                        {
                            if (DTISSUED.Rows[k]["OPQTY"].ToString() != "")
                            {
                                qty = qty + Convert.ToDouble(DTISSUED.Rows[k]["OPQTY"].ToString());
                            }
                            if (DTISSUED.Rows[k]["PURQTY"].ToString() != "")
                            {
                                qty = qty + Convert.ToDouble(DTISSUED.Rows[k]["PURQTY"].ToString());
                            }
                        }
                        txtIssuedQty.Text = qty.ToString();
                    }
                    double retrunqty = 0;
                    string sqlreturnqty = "select ISNULL(sum(IVR_RETURNITEM_BATCHDETAILS.OPQTY),0) as OPQTY,isnull(sum(IVR_RETURNITEM_BATCHDETAILS.PURQTY),0) as PURQTY from IVR_RETURNITEM_BATCHDETAILS inner join IVR_COMMONITEM_ISSUERETURN on IVR_RETURNITEM_BATCHDETAILS.RETURNBATCHID=IVR_COMMONITEM_ISSUERETURN.RETURNBATCHID where IVR_RETURNITEM_BATCHDETAILS.ITMID=" + hidItemId.Value + " and IVR_COMMONITEM_ISSUERETURN.SLIPNO=" + txtslipno.Text;
                    DataTable DTRETURN = DU.getdata(sqlreturnqty);
                    if (DTRETURN.Rows.Count > 0)
                    {
                        for (int k = 0; k < DTRETURN.Rows.Count; k++)
                        {
                            if (DTRETURN.Rows[k]["OPQTY"].ToString() != "")
                            {
                                retrunqty = retrunqty + Convert.ToDouble(DTRETURN.Rows[k]["OPQTY"].ToString());
                            }
                            if (DTRETURN.Rows[k]["PURQTY"].ToString() != "")
                            {
                                retrunqty = retrunqty + Convert.ToDouble(DTRETURN.Rows[k]["PURQTY"].ToString());
                            }
                        }
                        txtPrevReturn.Text = retrunqty.ToString();
                    }
                    txtBalance.Text = (Convert.ToDecimal(txtIssuedQty.Text) - Convert.ToDecimal(txtPrevReturn.Text)).ToString();
                }
            }
        }
    }
    protected void btnGetDetail_Click(object sender, EventArgs e)
    {
        pnl_Detail.Visible = true;
        GetData();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string forRetrunUpdate = "";
            //for IVR_RETURNITEM
            #region
            SqlParameter[] paramItem = { new SqlParameter("@SLIPNO",txtslipno.Text),
                                         new SqlParameter("@ISSUEDATE",Convert.ToDateTime(txtissuedate.Text)),
                                         new SqlParameter("@ISSUEBY",TxtissuedBy.Text),
                                         new SqlParameter("@RETURNDATE",Convert.ToDateTime(txtRetrunDate.Text)),
                                         new SqlParameter("@REQUESTEDBY",txtrequestedBy.Text),
                                         new SqlParameter("@RETURNTO",txtreturnedto.Text),
                                         new SqlParameter("@RETURNBY",txtRetrunBy.Text),
                                         new SqlParameter("@CNAME",Session["username"].ToString())
                                       };
            int result = DU.executeprocedure("USP_IVR_RETURNITEM", paramItem);
            #endregion
            //For IVR_RETURNITEM_DETAILS
            #region
            string getReturnID = "SELECT IDENT_CURRENT('IVR_RETURNITEM') AS ReturnID";
            DataTable DtReturnId = DU.getdata(getReturnID);
            if (DtReturnId.Rows.Count > 0)
            {
                for (int j = 1; j < tbldetail.Rows.Count; j++)
                {
                    #region Find the control
                    TextBox txtItemCode = (TextBox)tbldetail.Rows[j].FindControl("txtItemname" + j.ToString());
                    TextBox txtItemDesc = (TextBox)tbldetail.Rows[j].FindControl("txtItemdesc" + j.ToString());
                    HiddenField hidregid = (HiddenField)tbldetail.Rows[j].FindControl("hidregid" + j.ToString());
                    TextBox txtpecode = (TextBox)tbldetail.Rows[j].FindControl("txtPeCode" + j.ToString());
                    TextBox txtProductDesc = (TextBox)tbldetail.Rows[j].FindControl("txtproductdesc" + j.ToString());
                    TextBox txtRo = (TextBox)tbldetail.Rows[j].FindControl("txtro" + j.ToString());
                    TextBox txtcustomer = (TextBox)tbldetail.Rows[j].FindControl("txtcustomer" + j.ToString());
                    TextBox txtprojectname = (TextBox)tbldetail.Rows[j].FindControl("txtprojectname" + j.ToString());
                    HiddenField hidCustomerID = (HiddenField)tbldetail.Rows[j].FindControl("hidcustid" + j.ToString());
                    HiddenField hidProjectID = (HiddenField)tbldetail.Rows[j].FindControl("Hidprojectid" + j.ToString());
                    TextBox txtIssuedQty = (TextBox)tbldetail.Rows[j].FindControl("txtIssueQty" + j.ToString());
                    HiddenField hidItemId = (HiddenField)tbldetail.Rows[j].FindControl("HidItemId" + j.ToString());
                    HiddenField hidItemQty = (HiddenField)tbldetail.Rows[j].FindControl("HidItemQty" + j.ToString());
                    TextBox txtBalance = (TextBox)tbldetail.Rows[j].FindControl("txtBalance" + j.ToString());
                    TextBox txtPrevReturn = (TextBox)tbldetail.Rows[j].FindControl("txtPrevReturn" + j.ToString());
                    TextBox TxtReturnQty = (TextBox)tbldetail.Rows[j].FindControl("txtReturnQty" + j.ToString());
                    #endregion
                    #region for the Insert value
                    SqlParameter[] paramdetails = { 
                                                      new SqlParameter("@RETURNID",DtReturnId.Rows[0]["ReturnID"].ToString()),
                                                      new SqlParameter("@REGID",hidregid.Value),
                                                      new SqlParameter("@ITMID",hidItemId.Value),
                                                      new SqlParameter("@ITMCODE",txtItemCode.Text),
                                                      new SqlParameter("@ITEMDESC",txtItemDesc.Text),
                                                      new SqlParameter("@RO",txtRo.Text),
                                                      new SqlParameter("@CUSTOMER",hidCustomerID.Value),
                                                      new SqlParameter("@PROJECT",hidProjectID.Value),
                                                      new SqlParameter("@ISSUEQTY",txtIssuedQty.Text),
                                                      new SqlParameter("@RETURNQTY",TxtReturnQty.Text),
                                                      new SqlParameter("@CNAME",Session["username"].ToString())
                                                  };
                    int resultdetails = DU.executeprocedure("USP_IVR_RETURNITEM_DETAILS", paramdetails);
                    #endregion
                    string GetReturnDetail = "SELECT IDENT_CURRENT('IVR_RETURNITEM_DETAILS') AS ReturnDetailID";
                    DataTable DtReturnDetail = new DataTable();
                    DtReturnDetail = DU.getdata(GetReturnDetail);
                    #region Assign the Variable.
                    double taxamt = 0; double extendedvalue = 0; double unitpriceinr = 0; double invoicevalue = 0; double accessvalue = 0; double bcd = 0;
                    double cvd = 0; double educesscvd = 0; double heducesscvd = 0; double totalduty = 0; double customedu = 0; double Hcustomedu = 0;
                    double addduty = 0; double grossamount = 0; double purchasevalue = 0; double servicetax = 0; double servicetaxamount = 0;
                    double grandtotalduty = 0; double packaging = 0; double insurance = 0; double itmamt = 0; double netamt = 0; double freight = 0;
                    double taxper = 0;


                    double Ratetaxamt = 0; double Rateextendedvalue = 0; double Rateunitpriceinr = 0; double Rateinvoicevalue = 0; double Rateaccessvalue = 0; double Ratebcd = 0;
                    double Ratecvd = 0; double Rateeducesscvd = 0; double Rateheducesscvd = 0; double Ratetotalduty = 0; double Ratecustomedu = 0; double RateHcustomedu = 0;
                    double Rateaddduty = 0; double Rategrossamount = 0; double servicetaxvalue = 0; double servicetaxamountvalue = 0;
                    double grandtotaldutyvalue = 0; double packagingvalue = 0; double insurancevalue = 0; double itmamtvalue = 0;
                    double netamtvalue = 0; double freightvalue = 0; double taxpervalue = 0;
                    #endregion
                    DataTable DtReturnItemBatch = new DataTable();
                    if (DtReturnDetail.Rows.Count > 0)
                    {
                        //get the record from the IVR_ISSUEITEM_BATCHDETAILS
                        DataTable DTISSUEITEMBATCH = DU.getdata("select * from IVR_ISSUEITEM_BATCHDETAILS inner join IVR_ISSUEITEM_DETAILS on IVR_ISSUEITEM_BATCHDETAILS.ISSUEDETAILID=IVR_ISSUEITEM_DETAILS.ISSUEDETAILID where IVR_ISSUEITEM_DETAILS.REGID=" + hidregid.Value + " and IVR_ISSUEITEM_BATCHDETAILS.ITMID=" + hidItemId.Value);
                        if (DTISSUEITEMBATCH.Rows.Count > 0)
                        {
                            int ReturnQty = 0; int tempcheckqty = 0;
                            ReturnQty = Convert.ToInt32(TxtReturnQty.Text);
                            for (int i = DTISSUEITEMBATCH.Rows.Count; i > 0; i--)
                            {
                                //When the Purchase Id Is Exist.
                                #region
                                if (Convert.ToInt32(TxtReturnQty.Text) > 0)
                                {
                                    if (tempcheckqty != Convert.ToInt32(TxtReturnQty.Text))
                                    {
                                        //*******************************************
                                        //When The Item is Issued from the Purchase Table
                                        //*******************************************
                                        #region
                                        if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["PURCHASEID"].ToString()))
                                        {
                                            string id = DTISSUEITEMBATCH.Rows[i - 1]["PURCHASEID"].ToString();
                                            if (Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString()) > 0)
                                            {
                                                int PurQty = Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                //When Purchase Qty is Greater than Return Qty                                            
                                                if (PurQty > ReturnQty)
                                                {
                                                    #region Purchase qty is greater than Return Qty
                                                    //suppose 5>3;
                                                    //Then insert the Record.
                                                    #region Calculate the All Columns.
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["TAXAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXAMT"].ToString()) > 0)
                                                        {
                                                            Ratetaxamt = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXAMT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            taxamt = Ratetaxamt * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["EXTENDEDVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EXTENDEDVALUE"].ToString()) > 0)
                                                        {
                                                            Rateextendedvalue = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EXTENDEDVALUE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            extendedvalue = Rateextendedvalue * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["UNITPRICEINR"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["UNITPRICEINR"].ToString()) > 0)
                                                        {
                                                            Rateunitpriceinr = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["UNITPRICEINR"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            unitpriceinr = Rateunitpriceinr * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["INVOICEVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INVOICEVALUE"].ToString()) > 0)
                                                        {
                                                            Rateinvoicevalue = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INVOICEVALUE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            invoicevalue = Rateinvoicevalue * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["ACCESSVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ACCESSVALUE"].ToString()) > 0)
                                                        {
                                                            Rateaccessvalue = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ACCESSVALUE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            accessvalue = Rateaccessvalue * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["BCD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["BCD"].ToString()) > 0)
                                                        {
                                                            Ratebcd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["BCD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            bcd = Ratebcd * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["CVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CVD"].ToString()) > 0)
                                                        {
                                                            Ratecvd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CVD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            cvd = Ratecvd * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["EDUCESSONCVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EDUCESSONCVD"].ToString()) > 0)
                                                        {
                                                            Rateeducesscvd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EDUCESSONCVD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            educesscvd = Rateeducesscvd * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["HEDUCESSONCVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["HEDUCESSONCVD"].ToString()) > 0)
                                                        {
                                                            Rateheducesscvd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["HEDUCESSONCVD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            heducesscvd = Rateextendedvalue * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["TOTALDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TOTALDUTY"].ToString()) > 0)
                                                        {
                                                            Ratetotalduty = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TOTALDUTY"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            totalduty = Ratetotalduty * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["CUSTOMEEDU"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUSTOMEEDU"].ToString()) > 0)
                                                        {
                                                            Ratecustomedu = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUSTOMEEDU"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            customedu = Ratecustomedu * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["CUDTOMEHEDU"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUDTOMEHEDU"].ToString()) > 0)
                                                        {
                                                            RateHcustomedu = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUDTOMEHEDU"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            Hcustomedu = RateHcustomedu * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["ADDDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ADDDUTY"].ToString()) > 0)
                                                        {
                                                            Rateaddduty = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ADDDUTY"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            addduty = Rateaddduty * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) > 0)
                                                        {
                                                            Rategrossamount = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            grossamount = Rategrossamount * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }


                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAX"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAX"].ToString()) > 0)
                                                        {
                                                            servicetax = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAX"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            servicetaxvalue = servicetax * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAXAMOUNT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAXAMOUNT"].ToString()) > 0)
                                                        {
                                                            servicetaxamount = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAXAMOUNT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            servicetaxamountvalue = servicetaxamount * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["GRANDTOTALDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GRANDTOTALDUTY"].ToString()) > 0)
                                                        {
                                                            grandtotalduty = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GRANDTOTALDUTY"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            grandtotaldutyvalue = grandtotalduty * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["PACKAGING"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["PACKAGING"].ToString()) > 0)
                                                        {
                                                            packaging = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["PACKAGING"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            packagingvalue = packaging * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["INSURANCE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INSURANCE"].ToString()) > 0)
                                                        {
                                                            insurance = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INSURANCE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            insurancevalue = insurance * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["ITMAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ITMAMT"].ToString()) > 0)
                                                        {
                                                            itmamt = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ITMAMT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            itmamtvalue = itmamt * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["NETAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["NETAMT"].ToString()) > 0)
                                                        {
                                                            netamt = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["NETAMT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            netamtvalue = netamt * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["FREIGHT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["FREIGHT"].ToString()) > 0)
                                                        {
                                                            freight = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["FREIGHT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            freightvalue = freight * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["TAXPER"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXPER"].ToString()) > 0)
                                                        {
                                                            taxper = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXPER"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            taxpervalue = taxper * Convert.ToDouble(ReturnQty);
                                                        }
                                                    }
                                                    #endregion
                                                    SqlParameter[] ParamPurchase = { 
                                                                           new SqlParameter("@RETURNDETAILID",DtReturnDetail.Rows[0]["ReturnDetailID"].ToString()),
                                                                           new SqlParameter("@ITMID",DTISSUEITEMBATCH.Rows[i - 1]["ITMID"].ToString()),
                                                                           new SqlParameter("@PURCHASEID",id),
                                                                           new SqlParameter("@PURQTY",ReturnQty),
                                                                           new SqlParameter("@TAXAMT",taxamt),
                                                                           new SqlParameter("@EXTENDEDVALUE",extendedvalue),
                                                                           new SqlParameter("@UNITPRICEINR",unitpriceinr),
                                                                           new SqlParameter("@INVOICEVALUE",invoicevalue),
                                                                           new SqlParameter("@ACCESSVALUE",accessvalue),
                                                                           new SqlParameter("@BCD",bcd),
                                                                           new SqlParameter("@CVD",cvd),
                                                                           new SqlParameter("@EDUCESSONCVD",educesscvd),
                                                                           new SqlParameter("@HEDUCESSONCVD",heducesscvd),
                                                                           new SqlParameter("@TOTALDUTY",totalduty),
                                                                           new SqlParameter("@CUSTOMEEDU",customedu),
                                                                           new SqlParameter("@CUDTOMEHEDU",Hcustomedu),
                                                                           new SqlParameter("@ADDDUTY",addduty),
                                                                           new SqlParameter("@GROSSAMOUNT",grossamount),
                                                                           new SqlParameter("@CNAME",Session["username"].ToString()),
                                                                           new SqlParameter("@PURCHASEVALUE",Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["PURCHASEVALUE"].ToString())),
                                                                           new SqlParameter("@ENTRYTYPE","RET"),
                                                                           new SqlParameter("@SERVICETAX",servicetaxvalue),
                                                                           new SqlParameter("@SERVICETAXAMOUNT",servicetaxamountvalue),
                                                                           new SqlParameter("@GRANDTOTALDUTY",grandtotaldutyvalue),
                                                                           new SqlParameter("@PACKAGING",packagingvalue),
                                                                           new SqlParameter("@INSURANCE",insurancevalue),
                                                                           new SqlParameter("@ITMAMT",itmamtvalue),
                                                                           new SqlParameter("@NETAMT",netamtvalue),
                                                                           new SqlParameter("@FREIGHT",freightvalue),
                                                                           new SqlParameter("@TAXPER",taxpervalue)                                                                           
                                                                     };
                                                    int resultparam = DU.executeprocedure("USP_IVR_RETURNITEM_BATCHDETAILS", ParamPurchase);
                                                    tempcheckqty += ReturnQty;
                                                    forRetrunUpdate = ReturnQty.ToString();
                                                    #endregion
                                                }
                                                //When Purchase Qty is Less than Retrun Qty                                            
                                                double Tempremaining = 0; double temppurqty = 0;
                                                if (PurQty <= ReturnQty)
                                                {
                                                    #region for Purchase Qty is Less than Return Qty
                                                    Tempremaining = Convert.ToDouble(ReturnQty) - PurQty;
                                                    temppurqty = Convert.ToDouble(ReturnQty) - Tempremaining;
                                                    #region Calculate the All Columns.
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["TAXAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXAMT"].ToString()) > 0)
                                                        {
                                                            Ratetaxamt = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXAMT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            taxamt = Ratetaxamt * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["EXTENDEDVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EXTENDEDVALUE"].ToString()) > 0)
                                                        {
                                                            Rateextendedvalue = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EXTENDEDVALUE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            extendedvalue = Rateextendedvalue * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["UNITPRICEINR"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["UNITPRICEINR"].ToString()) > 0)
                                                        {
                                                            Rateunitpriceinr = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["UNITPRICEINR"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            unitpriceinr = Rateunitpriceinr * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["INVOICEVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INVOICEVALUE"].ToString()) > 0)
                                                        {
                                                            Rateinvoicevalue = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INVOICEVALUE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            invoicevalue = Rateinvoicevalue * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["ACCESSVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ACCESSVALUE"].ToString()) > 0)
                                                        {
                                                            Rateaccessvalue = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ACCESSVALUE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            accessvalue = Rateaccessvalue * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["BCD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["BCD"].ToString()) > 0)
                                                        {
                                                            Ratebcd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["BCD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            bcd = Ratebcd * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["CVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CVD"].ToString()) > 0)
                                                        {
                                                            Ratecvd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CVD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            cvd = Ratecvd * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["EDUCESSONCVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EDUCESSONCVD"].ToString()) > 0)
                                                        {
                                                            Rateeducesscvd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["EDUCESSONCVD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            educesscvd = Rateeducesscvd * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["HEDUCESSONCVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["HEDUCESSONCVD"].ToString()) > 0)
                                                        {
                                                            Rateheducesscvd = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["HEDUCESSONCVD"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            heducesscvd = Rateextendedvalue * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["TOTALDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TOTALDUTY"].ToString()) > 0)
                                                        {
                                                            Ratetotalduty = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TOTALDUTY"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            totalduty = Ratetotalduty * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["CUSTOMEEDU"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUSTOMEEDU"].ToString()) > 0)
                                                        {
                                                            Ratecustomedu = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUSTOMEEDU"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            customedu = Ratecustomedu * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["CUDTOMEHEDU"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUDTOMEHEDU"].ToString()) > 0)
                                                        {
                                                            RateHcustomedu = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["CUDTOMEHEDU"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            Hcustomedu = RateHcustomedu * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["ADDDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ADDDUTY"].ToString()) > 0)
                                                        {
                                                            Rateaddduty = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ADDDUTY"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            addduty = Rateaddduty * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) > 0)
                                                        {
                                                            Rategrossamount = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            grossamount = Rategrossamount * temppurqty;
                                                        }
                                                    }


                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAX"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAX"].ToString()) > 0)
                                                        {
                                                            servicetax = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAX"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            servicetaxvalue = servicetax * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAXAMOUNT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAXAMOUNT"].ToString()) > 0)
                                                        {
                                                            servicetaxamount = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["SERVICETAXAMOUNT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            servicetaxamountvalue = servicetaxamount * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["GRANDTOTALDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GRANDTOTALDUTY"].ToString()) > 0)
                                                        {
                                                            grandtotalduty = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GRANDTOTALDUTY"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            grandtotaldutyvalue = grandtotalduty * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["PACKAGING"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["PACKAGING"].ToString()) > 0)
                                                        {
                                                            packaging = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["PACKAGING"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            packagingvalue = packaging * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["INSURANCE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INSURANCE"].ToString()) > 0)
                                                        {
                                                            insurance = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["INSURANCE"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            insurancevalue = insurance * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["ITMAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ITMAMT"].ToString()) > 0)
                                                        {
                                                            itmamt = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["ITMAMT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            itmamtvalue = itmamt * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["NETAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["NETAMT"].ToString()) > 0)
                                                        {
                                                            netamt = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["NETAMT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            netamtvalue = netamt * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["FREIGHT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["FREIGHT"].ToString()) > 0)
                                                        {
                                                            freight = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["FREIGHT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            freightvalue = freight * temppurqty;
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["TAXPER"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXPER"].ToString()) > 0)
                                                        {
                                                            taxper = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["TAXPER"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["PURQTY"].ToString());
                                                            taxpervalue = taxper * temppurqty;
                                                        }
                                                    }
                                                    #endregion
                                                    #region
                                                    SqlParameter[] ParamPurchase = { 
                                                                           new SqlParameter("@RETURNDETAILID",DtReturnDetail.Rows[0]["ReturnDetailID"].ToString()),
                                                                           new SqlParameter("@ITMID",DTISSUEITEMBATCH.Rows[i - 1]["ITMID"].ToString()),
                                                                           new SqlParameter("@PURCHASEID",id),
                                                                           new SqlParameter("@PURQTY",temppurqty),
                                                                           new SqlParameter("@TAXAMT",taxamt),
                                                                           new SqlParameter("@EXTENDEDVALUE",extendedvalue),
                                                                           new SqlParameter("@UNITPRICEINR",unitpriceinr),
                                                                           new SqlParameter("@INVOICEVALUE",invoicevalue),
                                                                           new SqlParameter("@ACCESSVALUE",accessvalue),
                                                                           new SqlParameter("@BCD",bcd),
                                                                           new SqlParameter("@CVD",cvd),
                                                                           new SqlParameter("@EDUCESSONCVD",educesscvd),
                                                                           new SqlParameter("@HEDUCESSONCVD",heducesscvd),
                                                                           new SqlParameter("@TOTALDUTY",totalduty),
                                                                           new SqlParameter("@CUSTOMEEDU",customedu),
                                                                           new SqlParameter("@CUDTOMEHEDU",Hcustomedu),
                                                                           new SqlParameter("@ADDDUTY",addduty),
                                                                           new SqlParameter("@GROSSAMOUNT",grossamount),
                                                                           new SqlParameter("@CNAME",Session["username"].ToString()),
                                                                           new SqlParameter("@PURCHASEVALUE",Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["PURCHASEVALUE"].ToString())),
                                                                           new SqlParameter("@ENTRYTYPE","RET") ,
                                                                           new SqlParameter("@SERVICETAX",servicetaxvalue),
                                                                           new SqlParameter("@SERVICETAXAMOUNT",servicetaxamountvalue),
                                                                           new SqlParameter("@GRANDTOTALDUTY",grandtotaldutyvalue),
                                                                           new SqlParameter("@PACKAGING",packagingvalue),
                                                                           new SqlParameter("@INSURANCE",insurancevalue),
                                                                           new SqlParameter("@ITMAMT",itmamtvalue),
                                                                           new SqlParameter("@NETAMT",netamtvalue),
                                                                           new SqlParameter("@FREIGHT",freightvalue),
                                                                           new SqlParameter("@TAXPER",taxpervalue)      
                                                                     };
                                                    int resultparam = DU.executeprocedure("USP_IVR_RETURNITEM_BATCHDETAILS", ParamPurchase);
                                                    #endregion
                                                    tempcheckqty += Convert.ToInt32(temppurqty);
                                                    ReturnQty = Convert.ToInt32(Tempremaining);
                                                    forRetrunUpdate = temppurqty.ToString();
                                                    #endregion
                                                }
                                                string GetReturnItemBatchID = "SELECT IDENT_CURRENT('IVR_RETURNITEM_BATCHDETAILS') AS ReturnBatchID";
                                                DtReturnItemBatch = DU.getdata(GetReturnItemBatchID);
                                                //For Insert the Data into the Common Table.
                                                #region for Common Item Return
                                                SqlParameter[] ParamCommon = { 
                                                                             new SqlParameter("@RETURNID",DtReturnId.Rows[0]["ReturnID"].ToString()),
                                                                             new SqlParameter("@RETURNDETAILID",DtReturnDetail.Rows[0]["ReturnDetailID"].ToString()),
                                                                             new SqlParameter("@RETURNBATCHID",DtReturnItemBatch.Rows[0]["ReturnBatchID"].ToString()),
                                                                             new SqlParameter("@ITMID",DTISSUEITEMBATCH.Rows[i - 1]["ITMID"].ToString()),
                                                                             new SqlParameter("@PURCHASEID",id),
                                                                             new SqlParameter("@PURQTY",Convert.ToInt32(forRetrunUpdate)),
                                                                             new SqlParameter("@TAXAMT",taxamt),
                                                                             new SqlParameter("@EXTENDEDVALUE",extendedvalue),
                                                                             new SqlParameter("@UNITPRICEINR",unitpriceinr),
                                                                             new SqlParameter("@INVOICEVALUE",invoicevalue),
                                                                             new SqlParameter("@ACCESSVALUE",accessvalue),
                                                                             new SqlParameter("@CVD",cvd),
                                                                             new SqlParameter("@BCD",bcd),
                                                                             new SqlParameter("@EDUCESSONCVD",educesscvd),
                                                                             new SqlParameter("@HEDUCESSONCVD",heducesscvd),
                                                                             new SqlParameter("@TOTALDUTY",totalduty),
                                                                             new SqlParameter("@CUSTOMEEDUCESS",customedu),
                                                                             new SqlParameter("@CUSTOMEHEDUCESS",Hcustomedu),
                                                                             new SqlParameter("@ADDDUTY",addduty),
                                                                             new SqlParameter("@GROSSAMT",grossamount),
                                                                             new SqlParameter("@ENTRYTYPE","RET"),
                                                                             new SqlParameter("@SLIPNO",txtslipno.Text),
                                                                             new SqlParameter("@REGID",hidregid.Value),
                                                                             new SqlParameter("@RETURNQTY",Convert.ToInt32(forRetrunUpdate)),
                                                                             new SqlParameter("@RETURNVALUE",grossamount),
                                                                             new SqlParameter("@RETURNDATE",Convert.ToDateTime(txtRetrunDate.Text)),
                                                                             new SqlParameter("@SERVICETAX",servicetaxvalue),
                                                                             new SqlParameter("@SERVICETAXAMOUNT",servicetaxamountvalue),
                                                                             new SqlParameter("@GRANDTOTALDUTY",grandtotaldutyvalue),
                                                                             new SqlParameter("@PACKAGING",packagingvalue),
                                                                             new SqlParameter("@INSURANCE",insurancevalue),
                                                                             new SqlParameter("@ITMAMT",itmamtvalue),
                                                                             new SqlParameter("@NETAMT",netamtvalue),
                                                                             new SqlParameter("@FREIGHT",freightvalue),
                                                                             new SqlParameter("@TAXPER",taxpervalue)  
                                                                         };
                                                int resultcommon = DU.executeprocedure("USP_IVR_COMMONITEM_ISSUERETURN", ParamCommon);
                                                #endregion
                                                //for Insert the Purchase receive Table.
                                                #region Calculation for the Purchase Table
                                                #region for the Assign Variable
                                                double purfreight = 0; double puritmamt = 0; double purextendedvalue = 0; double purunitpriceinr = 0; double purinvoicevalue = 0; double puraccessvalue = 0;
                                                double purbasiccustomduty = 0; double purcvd = 0; double pureducessoncvd = 0; double purheducessoncvd = 0; double purcustomedu = 0;
                                                double purcustomhedu = 0; double purtotalduty = 0; double purgrandtotalduty = 0; double purrateincl = 0; double purgrossamount = 0; double puraddduty = 0;
                                                double purNetamt = 0; double purtaxper = 0; double purtaxamt = 0; double purpackaging = 0; double purinsurance = 0; double purservicetax = 0; double purservicetaxamount = 0;
                                                double purfreightvalue = 0; double puritmamtvalue = 0; double purextendedvaluevalue = 0; double purunitpriceinrvalue = 0; double purinvoicevaluevalue = 0; double puraccessvaluevalue = 0;
                                                double purbasiccustomdutyvalue = 0; double purcvdvalue = 0; double pureducessoncvdvalue = 0; double purheducessoncvdvalue = 0; double purcustomeduvalue = 0;
                                                double purcustomheduvalue = 0; double purtotaldutyvalue = 0; double purgrandtotaldutyvalue = 0; double purrateinclvalue = 0; double purgrossamountvalue = 0; double puradddutyvalue = 0;
                                                double purNetamtvalue = 0; double purtaxpervalue = 0; double purtaxamtvalue = 0; double purpackagingvalue = 0; double purinsurancevalue = 0; double purservicetaxvalue = 0; double purservicetaxamountvalue = 0;
                                                #endregion
                                                DataTable DtGetStock = DU.getdata("select * from IVR_TRAN_ITEM where TITID=" + id);
                                                if (DtGetStock.Rows.Count > 0)
                                                {
                                                    #region Calculation for the All Columns
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["FREIGHT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["FREIGHT"].ToString()) > 0)
                                                        {
                                                            purfreight = Convert.ToDouble(DtGetStock.Rows[0]["FREIGHT"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purfreightvalue = purfreight * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["ITMAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["ITMAMT"].ToString()) > 0)
                                                        {
                                                            puritmamt = Convert.ToDouble(DtGetStock.Rows[0]["ITMAMT"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            puritmamtvalue = puritmamt * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["extendedvalue"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["extendedvalue"].ToString()) > 0)
                                                        {
                                                            purextendedvalue = Convert.ToDouble(DtGetStock.Rows[0]["extendedvalue"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purextendedvaluevalue = purextendedvalue * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }

                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["UNITPRICEINR"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["UNITPRICEINR"].ToString()) > 0)
                                                        {
                                                            purunitpriceinr = Convert.ToDouble(DtGetStock.Rows[0]["UNITPRICEINR"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purunitpriceinrvalue = purunitpriceinr * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["INVOICEVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["INVOICEVALUE"].ToString()) > 0)
                                                        {
                                                            purinvoicevalue = Convert.ToDouble(DtGetStock.Rows[0]["INVOICEVALUE"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purinvoicevaluevalue = purinvoicevalue * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["ACCESSVALUE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["ACCESSVALUE"].ToString()) > 0)
                                                        {
                                                            puraccessvalue = Convert.ToDouble(DtGetStock.Rows[0]["ACCESSVALUE"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            puraccessvaluevalue = puraccessvalue * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["BASICCUSTOMDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["BASICCUSTOMDUTY"].ToString()) > 0)
                                                        {
                                                            purbasiccustomduty = Convert.ToDouble(DtGetStock.Rows[0]["BASICCUSTOMDUTY"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purbasiccustomdutyvalue = purbasiccustomduty * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["CVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["CVD"].ToString()) > 0)
                                                        {
                                                            purcvd = Convert.ToDouble(DtGetStock.Rows[0]["CVD"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purcvdvalue = purcvd * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["EDUCESSONCVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["EDUCESSONCVD"].ToString()) > 0)
                                                        {
                                                            pureducessoncvd = Convert.ToDouble(DtGetStock.Rows[0]["EDUCESSONCVD"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            pureducessoncvdvalue = pureducessoncvd * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["HEDUCESSONCVD"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["HEDUCESSONCVD"].ToString()) > 0)
                                                        {
                                                            purheducessoncvd = Convert.ToDouble(DtGetStock.Rows[0]["HEDUCESSONCVD"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purheducessoncvdvalue = purheducessoncvd * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["TOTALDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["TOTALDUTY"].ToString()) > 0)
                                                        {
                                                            purtotalduty = Convert.ToDouble(DtGetStock.Rows[0]["TOTALDUTY"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purtotaldutyvalue = purtotalduty * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["CUSTOMEDUCATIONAL"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["CUSTOMEDUCATIONAL"].ToString()) > 0)
                                                        {
                                                            purcustomedu = Convert.ToDouble(DtGetStock.Rows[0]["CUSTOMEDUCATIONAL"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purcustomeduvalue = purcustomedu * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["CUSTOMHEDU"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["CUSTOMHEDU"].ToString()) > 0)
                                                        {
                                                            purcustomhedu = Convert.ToDouble(DtGetStock.Rows[0]["CUSTOMHEDU"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purcustomheduvalue = purcustomhedu * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["ADDDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["ADDDUTY"].ToString()) > 0)
                                                        {
                                                            puraddduty = Convert.ToDouble(DtGetStock.Rows[0]["ADDDUTY"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            puradddutyvalue = puraddduty * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["GRANDTOTALDUTY"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["GRANDTOTALDUTY"].ToString()) > 0)
                                                        {
                                                            purgrandtotalduty = Convert.ToDouble(DtGetStock.Rows[0]["GRANDTOTALDUTY"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purgrandtotaldutyvalue = purgrandtotalduty * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["RATEINCL"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["RATEINCL"].ToString()) > 0)
                                                        {
                                                            purrateincl = Convert.ToDouble(DtGetStock.Rows[0]["RATEINCL"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purrateinclvalue = purrateincl * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["GROSSAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["GROSSAMT"].ToString()) > 0)
                                                        {
                                                            purgrossamount = Convert.ToDouble(DtGetStock.Rows[0]["GROSSAMT"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purgrossamountvalue = purgrossamount * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    //For Domestic
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["NETAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["NETAMT"].ToString()) > 0)
                                                        {
                                                            purNetamt = Convert.ToDouble(DtGetStock.Rows[0]["NETAMT"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purNetamtvalue = purNetamt * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["TAXPER"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["TAXPER"].ToString()) > 0)
                                                        {
                                                            purtaxper = Convert.ToDouble(DtGetStock.Rows[0]["TAXPER"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purtaxpervalue = purtaxper * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["TAXAMT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["TAXAMT"].ToString()) > 0)
                                                        {
                                                            purtaxamt = Convert.ToDouble(DtGetStock.Rows[0]["TAXAMT"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purtaxamtvalue = purtaxamt * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["PACKAGING"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["PACKAGING"].ToString()) > 0)
                                                        {
                                                            purpackaging = Convert.ToDouble(DtGetStock.Rows[0]["PACKAGING"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purpackagingvalue = purpackaging * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["INSURANCE"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["INSURANCE"].ToString()) > 0)
                                                        {
                                                            purinsurance = Convert.ToDouble(DtGetStock.Rows[0]["INSURANCE"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purinsurancevalue = purinsurance * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["SERVICETAX"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["SERVICETAX"].ToString()) > 0)
                                                        {
                                                            purservicetax = Convert.ToDouble(DtGetStock.Rows[0]["SERVICETAX"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purservicetaxvalue = purservicetax * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    if (!string.IsNullOrEmpty(DtGetStock.Rows[0]["SERVICETAXAMOUNT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DtGetStock.Rows[0]["SERVICETAXAMOUNT"].ToString()) > 0)
                                                        {
                                                            purservicetaxamount = Convert.ToDouble(DtGetStock.Rows[0]["SERVICETAXAMOUNT"].ToString()) / Convert.ToDouble(DtGetStock.Rows[0]["ITMQTY"].ToString());
                                                            purservicetaxamountvalue = purservicetaxamount * Convert.ToInt32(forRetrunUpdate);
                                                        }
                                                    }
                                                    #endregion
                                                    //For International
                                                    if (DtGetStock.Rows[0]["COO"].ToString() != "")
                                                    {
                                                        #region return qty is insert into the Purchase table with flag RET.
                                                        SqlParameter[] paramPurchase = {
                                                                                new SqlParameter("@TRNID",DtGetStock.Rows[0]["TRNID"].ToString()),
                                                                                new SqlParameter("@ITMID",DtGetStock.Rows[0]["ITMID"].ToString()),
                                                                                new SqlParameter("@ADDNDESC",DtGetStock.Rows[0]["ADDNDESC"].ToString()),
                                                                                new SqlParameter("@RATE",DtGetStock.Rows[0]["RATE"].ToString()),
                                                                                new SqlParameter("@ITMQTY","0"),                                      
                                                                                new SqlParameter("@FREIGHT",purfreightvalue),                                     
                                                                                new SqlParameter("@ITMAMT",puritmamtvalue),                                                                                
                                                                                new SqlParameter("@COO",DtGetStock.Rows[0]["COO"].ToString()),
                                                                                new SqlParameter("@EXTENDEDVALUE",purextendedvaluevalue),
                                                                                new SqlParameter("@UNITPRICEINR",purunitpriceinrvalue),
                                                                                new SqlParameter("@INVOICEVALUE",purinvoicevaluevalue),
                                                                                new SqlParameter("@ACCESSVALUE",puraccessvaluevalue),
                                                                                new SqlParameter("@BASICCUSTOMDUTY",purbasiccustomdutyvalue),
                                                                                new SqlParameter("@CVD",purcvdvalue),
                                                                                new SqlParameter("@EDUCESSONCVD",pureducessoncvdvalue),
                                                                                new SqlParameter("@HEDUCESSONCVD",purheducessoncvdvalue),
                                                                                new SqlParameter("@TOTALDUTY",purtotaldutyvalue),
                                                                                new SqlParameter("@CUSTOMEDUCATIONAL",purcustomeduvalue),
                                                                                new SqlParameter("@CUSTOMHEDU",purcustomheduvalue),
                                                                                new SqlParameter("@ADDDUTY",puradddutyvalue),
                                                                                new SqlParameter("@GRANDTOTALDUTY",purgrandtotaldutyvalue),
                                                                                new SqlParameter("@UOMID",DtGetStock.Rows[0]["UOMID"].ToString()),
                                                                                new SqlParameter("@RATEINCL",purrateinclvalue),
                                                                                new SqlParameter("@ALTITMQTY","0"),//Alternet item Qty Currently Not enable from Transaction Setting
                                                                                new SqlParameter("@GROSSAMT",purgrossamountvalue),
                                                                                new SqlParameter("@DISCPER","0"),
                                                                                new SqlParameter("@DISCAMT","0"),
                                                                                new SqlParameter("@CENVATPER","0"),
                                                                                new SqlParameter("@CENVATAMT","0"),
                                                                                new SqlParameter("@PRIMARYCESSPER","0"),
                                                                                new SqlParameter("@PRIMARYCESSAMT","0"),
                                                                                new SqlParameter("@HIGHERCESSPER","0"),
                                                                                new SqlParameter("@HIGHERCESSAMT","0"),
                                                                                new SqlParameter("@ITMPENDINGQTY","0"),
                                                                                new SqlParameter("@ITMAMT_LID","0"),
                                                                                new SqlParameter("@TAXAMT_LID","0"),
                                                                                new SqlParameter("@EntryType","RET"),
                                                                                new SqlParameter("@IssueQty","0"),
                                                                                new SqlParameter("@ReturnQty",Convert.ToInt32(forRetrunUpdate)),
                                                                                new SqlParameter("@ReturnDate",Convert.ToDateTime(txtRetrunDate.Text))
                                                                            };
                                                        int Result = DU.executeprocedure("USP_IVR_IMPORT_SaveTranItem", paramPurchase);
                                                        #endregion
                                                    }
                                                    //for Domestic Purchase
                                                    if (DtGetStock.Rows[0]["COO"].ToString() == "")
                                                    {
                                                        #region Domestic Purchase
                                                        SqlParameter[] param = {
                                                                            new SqlParameter("@TRNID",DtGetStock.Rows[0]["TRNID"].ToString()),
                                                                            new SqlParameter("@ITMID",DtGetStock.Rows[0]["ITMID"].ToString()),
                                                                            new SqlParameter("@ADDNDESC",DtGetStock.Rows[0]["ADDNDESC"].ToString()),
                                                                            new SqlParameter("@RATE",Convert.ToDecimal(DtGetStock.Rows[0]["RATE"].ToString())),
                                                                            new SqlParameter("@ITMQTY","0"),
                                                                            new SqlParameter("@NETAMT",purNetamtvalue),
                                                                            new SqlParameter("@TAXPER",purtaxpervalue),
                                                                            new SqlParameter("@TAXAMT",purtaxamtvalue),
                                                                            new SqlParameter("@FREIGHT",purfreightvalue),
                                                                            new SqlParameter("@PACKAGING",purpackagingvalue),
                                                                            new SqlParameter("@INSURANCE",purinsurancevalue),
                                                                            new SqlParameter("@ITMAMT",puritmamtvalue),                                                                            
                                                                            new SqlParameter("@UOMID",DtGetStock.Rows[0]["UOMID"].ToString()),
                                                                            new SqlParameter("@RATEINCL",purrateinclvalue),
                                                                            new SqlParameter("@TAX_TXMASID",DtGetStock.Rows[0]["TAX_TXMASID"].ToString()),
                                                                            new SqlParameter("@ALTITMQTY","0"),//Alternet item Qty Currently Not enable from Transaction Setting
                                                                            new SqlParameter("@GROSSAMT",purgrossamountvalue),
                                                                            new SqlParameter("@DISCPER","0"),
                                                                            new SqlParameter("@DISCAMT","0"),
                                                                            new SqlParameter("@CENVATPER","0"),
                                                                            new SqlParameter("@CENVATAMT","0"),
                                                                            new SqlParameter("@PRIMARYCESSPER","0"),
                                                                            new SqlParameter("@PRIMARYCESSAMT","0"),
                                                                            new SqlParameter("@HIGHERCESSPER","0"),
                                                                            new SqlParameter("@HIGHERCESSAMT","0"),
                                                                            new SqlParameter("@ITMPENDINGQTY","0"),
                                                                            new SqlParameter("@ITMAMT_LID","0"),
                                                                            new SqlParameter("@TAXAMT_LID","0"),
                                                                            new SqlParameter("@EntryType","RET"), 
                                                                            new SqlParameter("@ServiceTax",DtGetStock.Rows[0]["SERVICETAX"].ToString()),
                                                                            new SqlParameter("@ServiceTaxAmount",DtGetStock.Rows[0]["SERVICETAXAMOUNT"].ToString()),
                                                                            new SqlParameter("@ReturnQty",Convert.ToInt32(forRetrunUpdate)),
                                                                            new SqlParameter("@ReturnDate",Convert.ToDateTime(txtRetrunDate.Text)),
                                                                            new SqlParameter("@IssueQty","0")
                                                                        };
                                                        int insertPurchaseDomestic = DU.executeprocedure("USP_IVR_SaveTranItem", param);
                                                        #endregion
                                                    }
                                                }
                                                #endregion
                                            }
                                        }
                                        #endregion
                                        //********************************************
                                        //When the Item is Issued from Opening Balance.
                                        //********************************************
                                        #region
                                        if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["OPID"].ToString()))
                                        {
                                            string opid = DTISSUEITEMBATCH.Rows[i - 1]["OPID"].ToString();
                                            if (Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["OPQTY"].ToString()) > 0)
                                            {
                                                int OPQty = Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["OPQTY"].ToString());
                                                //When Opening Qty is Greater than Return Qty
                                                #region
                                                if (OPQty > ReturnQty)
                                                {
                                                    #region
                                                    //suppose 5>3;
                                                    //Then insert the Record.
                                                    #region Calculation
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) > 0)
                                                        {
                                                            Rategrossamount = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["OPQTY"].ToString());
                                                            grossamount = Rategrossamount * ReturnQty;
                                                        }
                                                    }
                                                    #endregion
                                                    SqlParameter[] ParamPurchase = { 
                                                                           new SqlParameter("@RETURNDETAILID",DtReturnDetail.Rows[0]["ReturnDetailID"].ToString()),
                                                                           new SqlParameter("@ITMID",DTISSUEITEMBATCH.Rows[i - 1]["ITMID"].ToString()),
                                                                           new SqlParameter("@OPID",opid),
                                                                           new SqlParameter("@OPQTY",ReturnQty),
                                                                           new SqlParameter("@OPVALUE",Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["OPVALUE"].ToString())),
                                                                           new SqlParameter("@OPDATE",Convert.ToDateTime(DTISSUEITEMBATCH.Rows[i - 1]["OPDATE"].ToString())),
                                                                           new SqlParameter("@GROSSAMOUNT",grossamount),
                                                                           new SqlParameter("@CNAME",Session["username"].ToString()),                                                                           
                                                                           new SqlParameter("@ENTRYTYPE","RET")                                                                           
                                                                     };
                                                    int resultparam = DU.executeprocedure("USP_IVR_RETURNITEM_BATCHDETAILS", ParamPurchase);
                                                    forRetrunUpdate = ReturnQty.ToString();
                                                    #endregion
                                                }
                                                #endregion
                                                //When Opening Qty is Less than Retrun Qty  
                                                double Tempopremaining = 0; double tempoppurqty = 0;
                                                #region
                                                if (OPQty <= ReturnQty)
                                                {
                                                    Tempopremaining = Convert.ToDouble(ReturnQty) - OPQty;
                                                    tempoppurqty = Convert.ToDouble(ReturnQty) - Tempopremaining;
                                                    #region Calculation
                                                    if (!string.IsNullOrEmpty(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()))
                                                    {
                                                        if (Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) > 0)
                                                        {
                                                            Rategrossamount = Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["GROSSAMOUNT"].ToString()) / Convert.ToInt32(DTISSUEITEMBATCH.Rows[i - 1]["OPQTY"].ToString());
                                                            grossamount = Rategrossamount * tempoppurqty;
                                                        }
                                                    }
                                                    #endregion
                                                    #region
                                                    SqlParameter[] ParamPurchase = { 
                                                                           new SqlParameter("@RETURNDETAILID",DtReturnDetail.Rows[0]["ReturnDetailID"].ToString()),
                                                                           new SqlParameter("@ITMID",DTISSUEITEMBATCH.Rows[i - 1]["ITMID"].ToString()),
                                                                           new SqlParameter("@OPID",opid),
                                                                           new SqlParameter("@OPQTY",tempoppurqty),
                                                                           new SqlParameter("@OPVALUE",Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["OPVALUE"].ToString())),
                                                                           new SqlParameter("@OPDATE",Convert.ToDateTime(DTISSUEITEMBATCH.Rows[i - 1]["OPDATE"].ToString())),
                                                                           new SqlParameter("@GROSSAMOUNT",grossamount),
                                                                           new SqlParameter("@CNAME",Session["username"].ToString()),                                                                           
                                                                           new SqlParameter("@ENTRYTYPE","RET")                                                                           
                                                                     };
                                                    int resultparam = DU.executeprocedure("USP_IVR_RETURNITEM_BATCHDETAILS", ParamPurchase);
                                                    #endregion
                                                    forRetrunUpdate = tempoppurqty.ToString();
                                                }
                                                DataTable DtReturnItemOPBatch = new DataTable();
                                                string GetReturnItemBatchID = "SELECT IDENT_CURRENT('IVR_RETURNITEM_BATCHDETAILS') AS ReturnBatchID";
                                                DtReturnItemOPBatch = DU.getdata(GetReturnItemBatchID);

                                                #endregion
                                                //For IVR_COMMONITEM_ISSUE_RETURN
                                                #region
                                                SqlParameter[] ParamOpDate ={
                                                                           new SqlParameter("@OPID",opid),
                                                                           new SqlParameter("@RETURNID",DtReturnId.Rows[0]["ReturnID"].ToString()),
                                                                           new SqlParameter("@RETURNDETAILID",DtReturnDetail.Rows[0]["ReturnDetailID"].ToString()),
                                                                           new SqlParameter("@RETURNBATCHID",DtReturnItemOPBatch.Rows[0]["ReturnBatchID"].ToString()),
                                                                           new SqlParameter("@ITMID",DTISSUEITEMBATCH.Rows[i-1]["ITMID"].ToString()),
                                                                           new SqlParameter("@OPQTY",forRetrunUpdate),
                                                                           new SqlParameter("@OPVALUE",Convert.ToDouble(DTISSUEITEMBATCH.Rows[i - 1]["OPVALUE"].ToString())),
                                                                           new SqlParameter("@OPDATE",Convert.ToDateTime(DTISSUEITEMBATCH.Rows[i - 1]["OPDATE"].ToString())),
                                                                           new SqlParameter("@GROSSAMT",grossamount),
                                                                           new SqlParameter("@ENTRYTYPE","RET"),
                                                                           new SqlParameter("@SLIPNO",txtslipno.Text),
                                                                           new SqlParameter("@REGID",hidregid.Value),
                                                                           new SqlParameter("@RETURNQTY",forRetrunUpdate),
                                                                           new SqlParameter("@RETURNVALUE",grossamount),
                                                                           new SqlParameter("@RETURNDATE",Convert.ToDateTime(txtRetrunDate.Text))                                                                           
                                                                       };
                                                int resultCommonOP = DU.executeprocedure("USP_IVR_COMMONITEM_ISSUERETURN", ParamOpDate);
                                                #endregion

                                                //for update the value.
                                                ReturnQty = Convert.ToInt32(Tempopremaining);


                                                //For Update the Itemopening Table
                                                string sqlOpretrun = "update IVR_ITEMOPENBAL set RETRUNQTY=ISNULL(RETRUNQTY,0) +" + Convert.ToInt32(forRetrunUpdate) + " where SLNUM=" + opid;
                                                int resultop = DU.executesql(sqlOpretrun);
                                            }
                                        }
                                        #endregion


                                        #region For Update the IVR_OnlineRegestionDetail.
                                        string reg = "select * from IVR_OnlineRegistration_Detail where ItemCode='" + txtItemCode.Text + "' and RegId=" + hidregid.Value;
                                        DataTable dtreg = DU.getdata(reg);
                                        if (dtreg.Rows.Count > 0)
                                        {
                                            string sqlregistration = "update IVR_OnlineRegistration_Detail set ReturnDate='" + Convert.ToDateTime(txtRetrunDate.Text).ToString("yyyy-MM-dd") + "',ReturnQty=ISNULL(ReturnQty,0) +" + Convert.ToInt32(forRetrunUpdate) + " where ID=" + dtreg.Rows[0]["ID"].ToString();
                                            int resultregistrion = DU.executesql(sqlregistration);
                                        }
                                        #endregion
                                    }
                                }
                                #endregion
                            }
                        }
                    }
                }
            }
            #endregion
            //For IVR_RETURNITEM_BATCHDETAILS
            #region




            #endregion
            Response.Redirect("IssuedItemReturn.aspx?mode=save");
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
    }
    protected void BindUsername()
    {
        string sqlusername = "select (ISNULL(FirstName,'') +' '+ ISNULL(lastName,'')) as USERNAME from Login where UserID='" + Session["userid"].ToString() + "'";
        DataTable dt = DU.getdata(sqlusername);
        if (dt.Rows.Count > 0)
        {
            txtRetrunBy.Text = dt.Rows[0]["USERNAME"].ToString().ToUpper();
            //txtreqdate.Text = System.DateTime.Now.ToShortDateString();            
        }
    }
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> ReturnTo(string prefixText)
    {
        Datautility db = new Datautility();
        DataTable dt = db.getdata("select ISNULL(Firstname,'')+' '+ ISNULL(lastname,'') as Name from login where Firstname  like '%" + prefixText + "%'");
        List<string> ReturnedTo = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ReturnedTo.Add(Convert.ToString(dt.Rows[i]["Name"]));
        }
        return ReturnedTo;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnl_Detail.Visible = false;
        txtslipno.Text = "";
    }
    private void ViewRetrunData()
    {
        DataTable DtRetrunDetail = DU.getdatausingprocedure("USP_IVR_GetItem_RetrunDetail");
        if (DtRetrunDetail.Rows.Count > 0)
        {
            grdView.DataSource = DtRetrunDetail;
            grdView.DataBind();
        }
    }
    protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView GrdReturnChild = (GridView)e.Row.FindControl("gvItemRetrunChild");
            string ReturnId = grdView.DataKeys[e.Row.RowIndex].Value.ToString();
            SqlParameter[] param = { new SqlParameter("@returnid", ReturnId) };
            DataTable DtRetrunItem = DU.getdatausingprocedure("USP_IVR_ReturnItemViewDetail", param);

            DataTable DTChildReturnItem = new DataTable();
            DTChildReturnItem.Columns.Add("ItemCode", typeof(string));
            DTChildReturnItem.Columns.Add("ItemDesc", typeof(string));
            DTChildReturnItem.Columns.Add("RO", typeof(string));
            DTChildReturnItem.Columns.Add("CustomerName", typeof(string));
            DTChildReturnItem.Columns.Add("projectname", typeof(string));
            DTChildReturnItem.Columns.Add("ReturnQTY", typeof(string));
            DTChildReturnItem.Columns.Add("IssueQty", typeof(string));
            if (DtRetrunItem.Rows.Count > 0)
            {
                for (int i = 0; i < DtRetrunItem.Rows.Count; i++)
                {
                    DataRow DR = DTChildReturnItem.NewRow();
                    DR["ItemCode"] = DtRetrunItem.Rows[i]["ITMCODE"].ToString();
                    DR["ItemDesc"] = DtRetrunItem.Rows[i]["ITEMDESC"].ToString();
                    DR["RO"] = DtRetrunItem.Rows[i]["RO"].ToString();
                    DR["CustomerName"] = DtRetrunItem.Rows[i]["CUSTOMER"].ToString();
                    DR["projectname"] = DtRetrunItem.Rows[i]["PROJECT"].ToString();
                    DR["ReturnQTY"] = DtRetrunItem.Rows[i]["RETURNQTY"].ToString();
                    DR["IssueQty"] = DtRetrunItem.Rows[i]["ISSUEQTY"].ToString();
                    DTChildReturnItem.Rows.Add(DR);
                }
            }
            GrdReturnChild.DataSource = DTChildReturnItem;
            GrdReturnChild.DataBind();
        }
    }
}